<!--
 * @Author: hyman
 * @Date: 2021-12-29 11:48:43
 * @LastEditors: hyman
 * @LastEditTime: 2021-12-30 12:30:43
 * @Description: 请填写简介:
-->
<template>
  <!-- 导航栏 -->
  <van-nav-bar title="确认订单" left-text="返回" left-arrow @click-left="back" />

  <!-- 收货地址 -->
  <!-- 有地址 -->
  <van-contact-card v-if="address" type="edit" :tel="address.mobile" :name="address.consignee" @click="onEdit" />

  <!-- 无地址 -->
  <van-contact-card v-else type="add" add-text="收货地址" @click="onAdd" />

  <!-- 购物车记录 -->
  <van-card v-for="(item) in cartlist" :key="item" :price="item.total" :num="item.nums"
      :desc="'单价：￥' + item.price" :title="item.product.name" :thumb="item.product.cover_cdn">
      <template #tags>
          <p>库存：{{ item.product.stock }}</p>
      </template>
  </van-card>

  <!-- 订单备注 -->
  <van-field v-model="content" rows="2" autosize label="订单备注" type="textarea" maxlength="500" placeholder="请输入订单备注"
      show-word-limit />

  <!-- 确认下单 -->
  <van-submit-bar :price="total" button-text="确认下单" @submit="onSubmit" />
</template>
<script>
export default {
  emits: ['toggle'],
  created() {
      this.$emit('toggle', false)

      // 购物车的ID
      this.cartids = this.$route.query.cartids ? this.$route.query.cartids : ''

      this.singcartids = this.$route.query.singcartids ? this.$route.query.singcartids : ''

      if (this.singcartids) {
          this.cartids = this.singcartids
      }

      if (!this.cartids) {
          this.$notify({
              message: '请输入下单商品',
              duration: 1000,
              onClose: () => {
                  //返回上一个界面
                  // this.$router.go(-1)
              }
          })

          return false
      }

      //获取购物车数据
      this.CartData()

      var address = this.$cookies.get('address')

      if (address) {
          this.address = address
      } else {
          //获取默认收货地址
          this.AddressData()
      }

  },
  data() {
      return {
          address: null,
          content: '',
          cartlist: [],
          total: 0,
          cartids: null,
          LoginUser: this.$cookies.get('LoginUser')
      }
  },
  computed: {
      // 合计总数
      total() {

          var total = 0

          this.cartlist.forEach((item) => {

              total += parseFloat(item.total)

          })
          total = parseFloat(total) * 100

          return total
      }
  },
  methods: {
      back() {
          this.$router.go(-1)
      },
      // 获取购物车数据
      async CartData() {

          var data = {
              userid: this.LoginUser.id,
              cartids: this.cartids
          }

          if (this.singcartids) {
              data.ispromptly = 1
          }

          var result = await this.$api.cartList(data)

          if (result.code = 1) {
              this.cartlist = result.data
          }

      },
      // 更改收货地址 
      onEdit() {
          this.$router.push({
              path: '/user/address/index',
              query: {
                  action: 'order'
              }
          })
      },
      // 添加收货地址
      onAdd() {
          this.$router.push({
              path: '/user/address/add',
          })
      },
      // 确认下单
      async onSubmit() {
          var data = {
              userid: this.LoginUser.id,
              cartids: this.cartids,
              content: this.content,
              addressid: this.address.id
          }
          if (this.singcartids) {
              data.ispromptly = 1
          }

          var result = await this.$api.orderAdd(data)

          if (result.code == '1') {
              this.$notify({
                  type: 'success',
                  message: result.msg,
                  duration: 1000,
                  onClose: () => {
                      //跳转到订单列表
                      this.$router.push('/user/base/index')
                      // this.$router.push({path:result.url})
                  }
              })
          } else {
              this.$notify({
                  message: result.msg,
                  duration: 1000,
              })
          }

      },
      // 获取收货地址
      async AddressData() {
          var result = await this.$api.AddressOrder({ userid: this.LoginUser.id })

          if (result.code == 0) {
              this.$notify({
                  message: result.msg,
                  duration: 1000,
              })
          }

          this.address = result.data
      }
  }
}
</script>